//
//  HMForgetController.m
//  HMMarket
//
//  Created by 孙鹏飞 on 2017/5/17.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMForgetController.h"
#import "HMTimingManager.h"
#import "CheckTool.h"

@interface HMForgetController ()

@property (weak, nonatomic) IBOutlet UITextField *mobileTextField; // 手机号
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;  // 验证码
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField; // 新密码
@property (weak, nonatomic) IBOutlet UIButton *codeBtn; // 获取验证码按钮
@property (weak, nonatomic) IBOutlet UIButton *resetBtn; // 重设密码按钮
@property (nonatomic, strong) HMTimingManager *codeTimingManager;  //倒计时管理
@property (nonatomic, strong) NSString * codeStr; // 短信验证码

@end

@implementation HMForgetController
static NSInteger const kCountDownNumber = 59;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"忘记密码";
    [self setupView];
    
    // 初始化倒计时显示。
    self.codeTimingManager = [HMTimingManager sharedTool];
    [self.codeTimingManager getCurrentTimesBlock:^(NSInteger result) {
        [self updateCodeBtnTitleWithResendSeconds:result];
    }];
}

// 点击获取验证码
- (IBAction)codeBtnClickHandler:(id)sender
{
    // 检测手机号
    if (![CheckTool checkMobile:self.mobileTextField.text]) {
        return;
    }
    
    // 获取验证码
    self.codeStr = [CheckTool getVerificationCode];
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"tel"] = self.mobileTextField.text;
    params[@"yzcode"] = self.codeStr;
    
    [HttpBaseTool post:kSMS params:params success:^(id responseObj) {
        MyLog(@"%@",responseObj[@"msg"]);
        if ([responseObj[@"code"] integerValue]== 200) {
            [HMHUDManager showHUDWithText:@"验证码已发送，请注意查收"];
            [self setupCodeTimer];
        } else {
            [HMHUDManager showHUDWithText:@"验证码发送失败,请重新获取"];
        }
    } failure:^(NSError *error) {
        
    }];
}

// 启动计时器
- (void)setupCodeTimer
{
    [self.codeBtn setTitle:[NSString stringWithFormat:@"%ld秒",(long)kCountDownNumber] forState:UIControlStateDisabled];
    self.codeBtn.enabled = NO;
    [self.codeBtn setTitleColor:[UIColor colorWithHexString:@"c9c9c9"] forState:UIControlStateNormal];
    self.codeBtn.backgroundColor = [UIColor colorWithHexString:@"f8f8f8"];
    self.codeTimingManager = [HMTimingManager sharedTool];
    [self.codeTimingManager initTimer:kCountDownNumber];
    [self.codeTimingManager getCurrentTimesBlock:^(NSInteger result) {
        [self updateCodeBtnTitleWithResendSeconds:result];
    }];
}

// 计时器结束
- (void)updateCodeBtnTitleWithResendSeconds:(NSInteger)seconds
{
    NSString *title = [NSString stringWithFormat:@"%ld秒",(long)seconds];
    if (seconds == 0) {
        // 倒计时结束
        self.codeBtn.enabled = YES;
        [self.codeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        self.codeBtn.backgroundColor = kMainColor;
        [self.codeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        return;
    }else{
        self.codeBtn.enabled = NO;
        [self.codeBtn setTitle:title forState:UIControlStateDisabled];
        return;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.codeTimingManager stopTimer];
    [super viewWillAppear:YES];
    
}

// 点击重置密码
- (IBAction)doResetPassword:(id)sender {
    // 检测手机号
    if (![CheckTool checkMobile:self.mobileTextField.text]) {
        return;
    }
    // 检测表单
    if (![self checkForm]) {
        return;
    }
    
    NSString *mobile = self.mobileTextField.text;
    NSString *password = self.passwordTextField.text;
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"tel"] = mobile;
    params[@"newPassword"] = password;
    params[@"yzm"] = self.codeTextField.text;
    
    [HttpBaseTool post:KResetPasswordUrl params:params success:^(id responseObj) {
        if ([responseObj[@"code"] integerValue]== 200) {
            [HMHUDManager showHUDWithText:@"密码重置成功"];
            [self.navigationController popViewControllerAnimated:YES];
        } else {
            [HMHUDManager showHUDWithText:@"密码重置失败"];
        }
    } failure:^(NSError *error) {
        
    }];
    
}


// 检测表单
- (BOOL)checkForm
{
    BOOL resetEnabled = YES;
    NSUInteger codeLength = self.codeTextField.text.length;
    NSUInteger passwordLength = self.passwordTextField.text.length;
    NSString * errorMsg;
    
    if (codeLength == 0) {
        errorMsg = @"验证码不能为空";
        resetEnabled = NO;
    } else if (passwordLength == 0) {
        errorMsg = @"请输入新密码";
        resetEnabled = NO;
    } else if (passwordLength < 6) {
        errorMsg = @"密码长度不得小于6位";
        resetEnabled = NO;
    } else if (passwordLength > 16) {
        errorMsg = @"密码长度不得大于16位";
        resetEnabled = NO;
    }
    if(!resetEnabled && [errorMsg length] > 0){
        [HMHUDManager showHUDWithText:errorMsg];
    }
    return resetEnabled;
}

- (void)setupView
{
    [self configButton:_codeBtn borderWidth:0 borderColor:nil];
    [self configButton:_resetBtn borderWidth:0 borderColor:nil];
}

- (void)configButton:(UIButton *)button  borderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor
{
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.layer.borderWidth = borderWidth;
    button.layer.borderColor = [borderColor CGColor];
}

@end
